﻿using RTW.OA.Models;
using RTW.OA.Servers;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace RTW.OA.Controllers
{
    public class HomeController : BaseController
    {
        //public dynamic menuservers;
        public MenuServers menuservers;
        public MessageServers messageServers;
        //private const string USER_NAME = "test02";

        public HomeController()
        {
            //Type type = Type.GetType("RTW.OA.Servers.MenuServers");
            //menuservers = IocUril.Resolve(type);
            menuservers = new MenuServers();
            messageServers = new MessageServers();
        }

        public ActionResult Index()
        {
            var users = Session["UserInfo"] as user;
            if (users != null)
            {
                ViewBag.User_NO = users.USER_NO;
                ViewBag.Messages = messageServers.GetByUserId(users.USER_NO);
            }
            return View();
        }

        public ActionResult AddMessage(message model)
        {
            try
            {
                model.MSG_ID = Guid.NewGuid();
                model.FROM_USER_NAME = this.CurrentUser != null ? this.CurrentUser.USER_NO : "";
                model.SEND_TIME = DateTime.Now;
                model.STATUS = 0;
                messageServers.Add(model);
                return Json(new
                {
                    code = 100,
                    data = model,
                    msg = "添加成功"
                }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                return Json(new
                {
                    code = 0,
                    msg = "添加失败，原因：" + e.Message
                }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult ReadMessage(string msgId)
        {
            try
            {
                messageServers.UpdateStatus(msgId);
                return Json(new
                {
                    code = 100,
                    msg = "设置成功"
                }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                return Json(new
                {
                    code = 0,
                    msg = "设置失败，原因：" + e.Message
                }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult Main()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
        [HttpGet]
        public ActionResult MyDesk()
        {
            return View();
        }
        
        public string InitDesk()
        {
            string DeskTopObjJson = "";
            List<DesktopObject> dList = new List<DesktopObject>();
            //dList.Add(new DesktopObject() { DtNum = 0, Index = "256", Title = "项目管理", DataUrl = "Project/ProjectManage.aspx?ProjectName=", MsgNum = 0, Img = "../../Content/img/desktop/project.png", MenuValue = "X001" });
            dList.Add(new DesktopObject() { DtNum = 0, Index = "4", Title = "收件箱", DataUrl = "Personal/EMail/Index/4", MsgNum = 0, Img = "../../Content/img/desktop/email.png", MenuValue = "001" });
            dList.Add(new DesktopObject() { DtNum = 0, Index = "16", Title = "修改密码", DataUrl = "Personal/Setting/ChangPwd", MsgNum = 0, Img = "../../Content/img/desktop/info.png", MenuValue = "012" });
            //dList.Add(new DesktopObject() { DtNum = 0, Index = "161", Title = "手机短信", DataUrl = "Mobile/MobileSms.aspx", MsgNum = 0, Img = "../../Content/img/desktop/sms.png", MenuValue = "097" });
            dList.Add(new DesktopObject() { DtNum = 0, Index = "6", Title = "单位公告通知", DataUrl = "Personal/Notify", MsgNum = 0, Img = "../../Content/img/desktop/notify.png", MenuValue = "004" });
            dList.Add(new DesktopObject() { DtNum = 0, Index = "128", Title = "车辆使用记录", DataUrl = "Common/Vehicle/CarUse", MsgNum = 0, Img = "../../Content/img/desktop/vehicle.png", MenuValue = "068" });
            dList.Add(new DesktopObject() { DtNum = 0, Index = "9", Title = "工作日志", DataUrl = "Personal/Diary", MsgNum = 0, Img = "../../Content/img/desktop/diary.png", MenuValue = "007" });
            dList.Add(new DesktopObject() { DtNum = 0, Index = "8", Title = "日程安排", DataUrl = "Personal/Calendar", MsgNum = 0, Img = "../../Content/img/desktop/calendar.png", MenuValue = "006" });
            dList.Add(new DesktopObject() { DtNum = 0, Index = "20", Title = "我的工作", DataUrl = "NWorkFlow/NWorkToDo.aspx", MsgNum = 0, Img = "../../Content/img/desktop/default.png", MenuValue = "015" });
            dList.Add(new DesktopObject() { DtNum = 0, Index = "76", Title = "会议查询", DataUrl = "Meeting/Meeting", MsgNum = 0, Img = "../../Content/img/desktop/meeting.png", MenuValue = "065" });
            //dList.Add(new DesktopObject() { DtNum = 0, Index = "190", Title = "图书管理", DataUrl = "Office/Book.aspx", MsgNum = 0, Img = "../../Content/img/desktop/book.png", MenuValue = "120" });
            dList.Add(new DesktopObject() { DtNum = 0, Index = "47", Title = "资产管理", DataUrl = "Common/Asset", MsgNum = 0, Img = "../../Content/img/desktop/asset.png", MenuValue = "039" });
            //dList.Add(new DesktopObject() { DtNum = 0, Index = "185", Title = "论坛BBS", DataUrl = "BBS/BanKuaiList.aspx", MsgNum = 0, Img = "../../Content/img/desktop/bbs.png", MenuValue = "116" });
            //dList.Add(new DesktopObject() { DtNum = 0, Index = "26", Title = "工作委托", DataUrl = "NWorkFlow/WorkWT.aspx", MsgNum = 0, Img = "../../Content/img/desktop/workflow.png", MenuValue = "021" });
            //dList.Add(new DesktopObject() { DtNum = 0, Index = "53", Title = "题库管理", DataUrl = "DocFile/TiKuFrame.aspx", MsgNum = 0, Img = "../../Content/img/desktop/exam_manage.png", MenuValue = "044" });
            //dList.Add(new DesktopObject() { DtNum = 0, Index = "55", Title = "在线考试", DataUrl = "DocFile/TiKuKaoShi.aspx", MsgNum = 0, Img = "../../Content/img/desktop/score.png", MenuValue = "046" });
            //dList.Add(new DesktopObject() { DtNum = 0, Index = "36", Title = "个人文件柜", DataUrl = "DocCenter/DocCenter.aspx?Type=个人文件&DirID=0", MsgNum = 0, Img = "../../Content/img/desktop/file_folder.png", MenuValue = "029" });
            //dList.Add(new DesktopObject() { DtNum = 0, Index = "205", Title = "报表管理", DataUrl = "ReportCenter/ReportFrame.aspx", MsgNum = 0, Img = "../../Content/img/desktop/reportshop.png", MenuValue = "131" });
            //dList.Add(new DesktopObject() { DtNum = 0, Index = "61", Title = "人事档案", DataUrl = "System/RenShiInfo.aspx", MsgNum = 0, Img = "../../Content/img/desktop/hr.png", MenuValue = "051" });
            dList.Add(new DesktopObject() { DtNum = 0, Index = "13", Title = "个人通讯薄", DataUrl = "Personal/Address", MsgNum = 0, Img = "../../Content/img/desktop/address.png", MenuValue = "010" });
            dList.Add(new DesktopObject() { DtNum = 0, Index = "172", Title = "考勤明细", DataUrl = "Common/Attend/Evection", MsgNum = 0, Img = "../../Content/img/desktop/attendance.png", MenuValue = "107" });
            dList.Add(new DesktopObject() { DtNum = 0, Index = "165", Title = "收文阅读", DataUrl = "Document/Receive", MsgNum = 0, Img = "../../Content/img/desktop/document.png", MenuValue = "101" });
            dList.Add(new DesktopObject() { DtNum = 0, Index = "191", Title = "借阅管理", DataUrl = "Document/Borrow", MsgNum = 0, Img = "../../Content/img/desktop/roll_manage.png", MenuValue = "121" });
            //dList.Add(new DesktopObject() { DtNum = 0, Index = "7", Title = "投票", DataUrl = "GongGao/Vote.aspx", MsgNum = 0, Img = "../../Content/img/desktop/vote.png", MenuValue = "005" });
            dList.Add(new DesktopObject() { DtNum = 0, Index = "176", Title = "工作计划", DataUrl = "Personal/WorkPlan", MsgNum = 0, Img = "../../Content/img/desktop/work_plan.png", MenuValue = "109" });

            foreach (DesktopObject o in dList)
            {
                //if (FTD.Unit.PublicMethod.StrIFIn("|" + o.MenuValue + "|", FTD.Unit.PublicMethod.GetSessionValue("QuanXian")) == true)
                {
                    if (string.IsNullOrEmpty(DeskTopObjJson) == true)
                    {
                        DeskTopObjJson = o.GetJsonStr();
                    }
                    else
                    {
                        DeskTopObjJson = DeskTopObjJson + "," + o.GetJsonStr();
                    }
                }
            }

            DeskTopObjJson = "[" + DeskTopObjJson + "]";
            return DeskTopObjJson;
        }

        public ActionResult InitMenu()
        {
            string menustr = menuservers.InitMenu().ToString();
            return Json(menustr, JsonRequestBehavior.AllowGet);
        }
    }
}